/**
 * This file is part of the Kompics P2P Framework.
 *
 * Copyright (C) 2009 Swedish Institute of Computer Science (SICS)
 * Copyright (C) 2009 Royal Institute of Technology (KTH)
 *
 * Kompics is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package se.sics.drsm.components;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import se.sics.drsm.inits.StateMachineInit;
import se.sics.drsm.messages.UpdateConfiguration;
import se.sics.drsm.model.DRSMAddress;
import se.sics.drsm.model.StateMachineConfiguration;
import se.sics.drsm.ports.statemachine.StateMachinePort;
import se.sics.drsm.ports.statemachine.events.AddPeer;
import se.sics.drsm.ports.statemachine.events.ReConfigurationRequest;
import se.sics.drsm.ports.statemachine.events.UpdatePeers;
import se.sics.kompics.ComponentDefinition;
import se.sics.kompics.Handler;
import se.sics.kompics.Negative;
import se.sics.kompics.Positive;
import se.sics.kompics.network.Network;

import java.util.List;

/**
 * The <code>StateMachine</code> class.
 *
 * @author Shadid Chowdhury <msrch@kth.se>
 */
public final class StateMachine extends ComponentDefinition {

    Negative<StateMachinePort> stateMachinePort = negative(StateMachinePort.class);
    Positive<Network> network = positive(Network.class);

    private StateMachineConfiguration machineConfiguration;

    private Logger logger = LoggerFactory.getLogger(getClass().getName());


    public StateMachine() {
        machineConfiguration = null;

        subscribe(handleInit, control);
        subscribe(handleAddPeer, stateMachinePort);
        subscribe(handleUpConfigurationRequest, network);
    }

    Handler<StateMachineInit> handleInit = new Handler<StateMachineInit>() {
        public void handle(StateMachineInit init) {
            logger.debug("StateMachineInit");
//            logger = LoggerFactory.getLogger(getClass().getName() + "@"
//                    + self.getKey().getId());
        }
    };

    private Handler<AddPeer> handleAddPeer = new Handler<AddPeer>() {
        public void handle(AddPeer event) {
            logger.debug("handleAddPeer {}", event.toString());
            List<DRSMAddress> peerList = machineConfiguration.getPeerList();
            peerList.add(event.getNewPeer());
            ReConfigurationRequest reConfigurationRequest = new ReConfigurationRequest(
                    event.getSender(), new StateMachineConfiguration(machineConfiguration.getLeader(), peerList,
                    event.getNewPeer(), null));
            trigger(reConfigurationRequest, stateMachinePort);
        }
    };

    private Handler<UpdateConfiguration> handleUpConfigurationRequest = new Handler<UpdateConfiguration>() {
        public void handle(UpdateConfiguration event) {
            logger.debug("handleUpConfigurationRequest");
            machineConfiguration = event.getMachineConfiguration();

            //Update other components about the new peer
            UpdatePeers updatePeers = new UpdatePeers(event.getMachineConfiguration().getLeader(),
                    event.getMachineConfiguration().getPeerList());
            trigger(updatePeers, stateMachinePort);

            if (machineConfiguration.getAddedPeer() != null)
                //response back to added peer or else do nothing
                logger.info("New Configuration {}", machineConfiguration);
        }
    };


}
